package pub.tbc.atps.process;

import lombok.extern.slf4j.Slf4j;
import pub.tbc.atps.config.PropConfig;
import pub.tbc.atps.task.RestTask;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author tbc on 2016/11/29 13:28:53.
 */
@Slf4j
public class HandlerInvoker {
    /**
     * 线程总数
     */
    private int threadCount = PropConfig.getInt("task.thread-count");

    private ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(threadCount);
//    private ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool();

    private static HandlerInvoker invoker = new HandlerInvoker();

    //
    private HandlerChain getHandler(RestTask task) {
        return new HandlerChain(task);
    }

    public void invoke(RestTask task) {
        HandlerChain chain = getHandler(task);
        log.debug("getPoolSize: {}", threadPool.getPoolSize());
        log.debug("isTerminated: {}", threadPool.isTerminated());
        log.debug("getCorePoolSize: {}", threadPool.getCorePoolSize());
        log.debug("getMaximumPoolSize: {}", threadPool.getMaximumPoolSize());
        log.debug("getActiveCount: {}", threadPool.getActiveCount());
        log.debug("getCompletedTaskCount: {}", threadPool.getCompletedTaskCount());


        threadPool.execute(chain);
//        log.info("task: -> {}", task);

    }

    public static void process(RestTask task) {
        invoker.invoke(task);
    }

}
